﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace TotalSport
{
    public partial class AddProducts : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            BAL.conn.connectionString = ConfigurationManager.ConnectionStrings["TotalSport"].ConnectionString;
            if (Session["UserId"] == null || Session["UserId"].ToString() == "")
            {
                Session.Abandon();
                Session.RemoveAll();
                Response.Redirect("LoginUser.aspx");
            }
            if (!IsPostBack)
            {
                //txtParticularName.Style.Add("display", "none");
                //BAL.Customers.PopulateParticularsName(ref ddlParticularName);
                //BAL.Customers.PopulateCategBory(ref ddlCategory);
                //BAL.Customers.PopulateSubCategBory(ref ddlsubCategory,category);

                DisablesubCategory();
                DisableParticulrName();
                for (int i = 1; i <= 10; i++)
                {
                    ddlsize.Items.Add(i.ToString());
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (lnkAdd.InnerText == "done")
            {
                ddlCategory.Enabled = false;
                ddlsubCategory.Enabled = false;
                ddlParticularName.Enabled = false;
                //txtParticularName.Enabled = false;
            }
            else if (ddlCategory.SelectedValue == "-1")
            {
                ddlCategory.Focus();
                spncategory.Attributes.Add("style", "Display:block");
                spncategory.InnerHtml = "Please,Select Category";
            }
            else if (ddlsubCategory.SelectedValue == "-1")
            {
                ddlsubCategory.Focus();
                spnSubcategory.Attributes.Add("style", "Display:block");
                spnSubcategory.InnerHtml = "Please,Select SubCategory";                
            }
            else if (ddlParticularName.SelectedValue == "-1")
            {               
                ddlParticularName.Focus();
                spnParticularNm.Attributes.Add("style", "Display:block");
                spnParticularNm.InnerHtml = "Please,Select Particular Name";
            }
            else
            {
                string category = ddlCategory.SelectedValue;
                string subcategory = ddlsubCategory.SelectedValue;
                string ParticularsName = ddlParticularName.SelectedValue;

                //if (ddlParticularName.SelectedValue == "-1")
                //{
                //    ParticularsName = txtParticularName.Text;
                //}
                //else
                //{
                //    ParticularsName = ddlParticularName.Text;
                //}

                string BrandName = txtBrandName.Text;
                string ItemCode = txtItemCode.Text;
                string size = ddlsize.SelectedValue;
                string vat = ddlvat.SelectedValue;
                string fularge = FUploadLarge.FileName;
                if (fularge == "")
                {
                    fularge = lblhiddentxt.Text;
                }
                else
                {
                    fularge = FUploadLarge.FileName;
                    FUploadLarge.SaveAs(Server.MapPath("Images/" + fularge));
                }

                string Pic = FUpload.FileName;

                if (Pic == "")
                {


                    Pic = lblhiddentxt.Text;
                }
                else
                {
                    Pic = FUpload.FileName;
                    FUpload.SaveAs(Server.MapPath("Images/" + Pic));
                }
                string Price = txtPrice.Text;
                string Description = txtDescription.Text;
                BAL.Customers.InsertProductDetails(category, ParticularsName, BrandName,
                    ItemCode, Pic, Price, Description, subcategory, size, vat, fularge);

                //BAL.Customers.PopulateParticularsName(ref ddlParticularName);

                spnMsg.Attributes.Add("style", "Display:block");
                spnMsg.InnerHtml = "Record Saved Successfully";

                txtBrandName.Text = "";
                txtItemCode.Text = "";
                txtPrice.Text = "";
                txtDescription.Text = "";
                //txtParticularName.Text = "";
                ddlParticularName.ClearSelection();
                ddlCategory.ClearSelection();
                ddlsize.ClearSelection();
                ddlvat.ClearSelection();
                ddlsubCategory.ClearSelection();

            }



        }
        protected void ddlCategory_Init(object sender, EventArgs e)
        {
            spnMsg.Style.Add("display", "none");
        }

        protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            spncategory.Attributes.Add("display", "none");
            spncategory.InnerHtml = "";
            if (ddlCategory.SelectedIndex != 0)
            {
                ddlParticularName.Enabled = false;
                ddlsubCategory.Enabled = true;
                string category = ddlCategory.SelectedValue;
                BAL.Customers.PopulateSubCategBory1(ref ddlsubCategory, category);
            }
            else
            {
                DisablesubCategory();
                DisableParticulrName();
            }

        }
        public void DisablesubCategory()
        {
            BAL.Customers.PopulateCategBory(ref ddlCategory);
            System.Web.UI.WebControls.ListItem lst = new System.Web.UI.WebControls.ListItem("----Select SubCategory----", "-1");
            ddlsubCategory.Items.Insert(0, lst);
            ddlsubCategory.Enabled = false;
            ddlsubCategory.SelectedIndex = -1;
        }
        public void DisableParticulrName()
        {
            System.Web.UI.WebControls.ListItem lstPname = new System.Web.UI.WebControls.ListItem("----Select Particular's Name----", "-1");
            ddlParticularName.Items.Insert(0, lstPname);
            ddlParticularName.SelectedIndex = -1;
            ddlParticularName.Enabled = false;

        }

        protected void ddlsubCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            spnSubcategory.Attributes.Add("display", "none");
            spnSubcategory.InnerHtml = "";
            if (ddlsubCategory.SelectedIndex != 0)
            {
                ddlParticularName.Enabled = true;
                string category = ddlCategory.SelectedValue;
                string subcategory = ddlsubCategory.SelectedValue;
                BAL.Customers.populateParticularname1(ref ddlParticularName, category, subcategory);
            }

        }

        protected void ddlParticularName_SelectedIndexChanged(object sender, EventArgs e)
        {
            spnParticularNm.Attributes.Add("display", "none");
            spnParticularNm.InnerHtml = "";
        }







    }
}